<?php

namespace app\controllers\supplier\uni\finance;

use app\controllers\supplier\base\AdminController;
use app\controllers\pay\ForController;
use app\models\order\Order;
use app\models\pay\Pay;
use app\models\supplier\SupplierBank;
use app\models\system\SetBasic;
use app\models\user\UserAccount;
use app\models\supplier\SupplierCash as Model;

class CashController extends AdminController
{
    public function actionList()
    {
        // 检测绑定银行卡
        $is = SupplierBank::getLastOne($this->user->id, true);
        if (!$is) {
            return $this->fail('您还没有绑定银行卡,请到帐户管理绑定');
        }


        // 总进帐流水, 提现完成总数
        // $order_sn = Order::find()
        //     ->where(['supplier_id' => $this->user->id])
        //     ->select(['sn'])
        //     ->column();
        // $total_amount = Pay::find()
        //     ->where(['status' => 1])
        //     ->andWhere(['sn' => $order_sn])
        //     ->sum('amount');
        $total_amount = Order::find()
            ->where(['supplier_id' => $this->user->id])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->sum('total_pay');

        $total_cash = Model::find()
            ->where(['status' => 1])
            ->andWhere(['user_id' => $this->user->id])
            ->sum('amount');
        $total_cash_check = Model::find()
            ->where(['status' => 0])
            ->andWhere(['user_id' => $this->user->id])
            ->sum('amount');


        $sn = $this->post('sn');
        $user_id = $this->post('user_id');
        $export = $this->post('export');

        $start = $this->post('start');
        $end = $this->post('end');

        $status = $this->post('status');

        $mobile = $this->post('mobile');
        $nickname = $this->post('nickname');

        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $sort = $this->post('sort');

        $query = Model::find()
            ->where(['user_id' => $this->user->id]);

        if ($status > -2) {
            $query->andWhere(['status' => $status]);
        }

        if ($sort) {
            $prop = substr($sort, 0, 1);
            $prop = $prop == '-' ? SORT_DESC : SORT_ASC;
            $order = substr($sort, 1);
            $query->orderBy([$order => $prop]);
        } else {
            $query->orderBy(['id' => SORT_DESC]);
        }

        if ($user_id) {
            $query->andWhere(['user_id' => $user_id]);
        }
        if ($sn) {
            $query->andWhere(['like', 'sn', $sn]);
        }
        if ($mobile) {
            $query->andWhere(['like', 'mobile', $mobile]);
        }
        if ($nickname) {
            $query->andWhere(['like', 'nickname', $nickname]);
        }

        // if ($start) {
        //     $query->andWhere(['>=', 'create_time', $start]);
        // }
        // if ($end) {
        //     $query->andWhere(['<=', 'create_time', $end]);
        // }
        if ($start) {
            // $query->andWhere(['>=', 'create_time', $start]);
            $query->andWhere(['>=', 'DATE_FORMAT(create_time, "%Y-%m-%d")', date('Y-m-d', strtotime($start))]);
        }
        if ($end) {
            // $query->andWhere(['<=', 'create_time', $end]);
            $query->andWhere(['<=', 'DATE_FORMAT(create_time, "%Y-%m-%d")', date('Y-m-d', strtotime($end))]);
        }

        // export
        if ($export) {
            return $this->success($query->asArray()->all());
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->asArray()
            ->all();

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
            'total_amount'  => (float)$total_amount,
            'total_cash'  => (float)$total_cash,
            'total_cash_check'  => (float)$total_cash_check,
            'set' => SetBasic::getLastOne(),
        ]);

    }

    public function actionPayFor()
    {
        $id = $this->post('id');
        $model = Model::findOne(['id' => $id]);

        if (!$model) {
            return $this->fail('操作错误');
        }

        if ($model->status == 1) {
            return $this->fail('不要重复支付');
        }

        if ($model->amount <= 0) {
            return $this->fail('提现金额错误');
        }

        // manual ops
        $model->status = 1;
        $model->save();
        return $this->success();

        // pay for
        $result = ForController::pay(
            $model->sn,
            $model->user_id,
            ($model->amount - $model->service),
            $model->bank_number,
            $model->nickname,
            1,
            '提现'
        );

        if ($result) {

            // $model->status = 1;
            // $model->save();

            return $this->success();
        }

        return $this->fail('请求支付失败');

    }

    public function actionReject()
    {
        $id = $this->post('id');
        $txt = $this->post('txt');

        $model = Model::findOne(['id' => $id]);
        if (!$model || $model->status == -1) {
            return $this->fail('操作错误');
        }

        $model->status = -1;
        $model->reject = $txt;
        $model->save();

        $account = UserAccount::findOne(['user_id' => $model->user_id]);
        $account->balance += $model->amount;
        $account->save();

        return $this->success();
    }
}